﻿using Gameplay.PVE.Utils;
using System.Collections.Generic;
using UnityEngine;
using Yoozoo.Framework.Managers;
using Yoozoo.Gta.Common;

namespace NpcFramework
{
    public class NpcDialogInfo
    {
        public int eventId;
        public int[] npcIds;
        public Queue<NpcDialogContentInfo> contentQueue;
        public int currentIndex;
        public int npcNum;
        public int headIndex;
        public int headNpcId;
        private bool isInitDialogConfig = false;
        public bool StartPlayed = false;
        public void UpdateDialogContent(Dictionary<int, RpgNpcdialog> dialogConfig)
        {
            isInitDialogConfig = true;
            contentQueue = new Queue<NpcDialogContentInfo>(dialogConfig.Count);
            currentIndex = 1;
            var enumerator = dialogConfig.GetEnumerator();
            while (enumerator.MoveNext())
            {
                var index = enumerator.Current.Key;
                var value = enumerator.Current.Value;


                var contentConfig = PveUtils.GetNpcdialogContent((int)value.dialogContent.y);
                int npcIdIndex = (int)(value.dialogContent.x);
                int npcId = -666;
                if (npcIdIndex == 1)
                {
                    npcId = NpcDataManager.MainCharacterId; //主角
                }
                else
                {
                    npcId = npcIds[npcIdIndex - 2];
                }
                var contentInfo = new NpcDialogContentInfo
                {
                    index = value.order,
                    playTime = contentConfig.playTime,
                    content = contentConfig.content,
                    animName = contentConfig.animName,
                    npcId = npcId
                };
                contentQueue.Enqueue(contentInfo);
            }
            enumerator.Dispose();
            var firstContent = contentQueue.Peek();
            headIndex = firstContent.index;
            headNpcId = firstContent.npcId;
        }

        public bool IsCanPlay(int npcId)
        {
            return currentIndex == headIndex && npcId == headNpcId;
        }

        public bool IsFinish()
        {
            return isInitDialogConfig && contentQueue.Count == 0;
        }

        public void GoNextDialog()
        {
            if (contentQueue.Count >0)
            {
                currentIndex++;
                var firstContent = contentQueue.Peek();
                headIndex = firstContent.index;
                headNpcId = firstContent.npcId;
            }
        }
        public NpcDialogContentInfo GetCurrentDialog()
        {
            StartPlayed = true;
            var contentInfo = contentQueue.Dequeue();
            return contentInfo;
        }

        public void Dispose()
        {
            if (contentQueue!=null)
            {
                contentQueue.Clear();
                contentQueue = null;
            }
        }
    }

    public class NpcDialogContentInfo
    {
        public int index;
        public int id;
        public int npcId;
        public bool hasPlayed = false;
        public float playTime;
        public string animName;
        public string content;
    }
    public class NpcDialogManager
    {
       
        private Dictionary<int, NpcDialogInfo> dialogDatas = new Dictionary<int, NpcDialogInfo>(4);

        public void Dispose()
        {
            foreach (var dialogData in dialogDatas.Values)
            {
                dialogData.Dispose();
                dialogDatas = null;
            }
        }
        public NpcDialogManager()
        {

        }
        public void RegisterDialog(int pointIndex,List<int> npcIds)
        {
            if (!dialogDatas.ContainsKey(pointIndex))
            {
                var diaologInfo = new NpcDialogInfo();
                diaologInfo.npcIds = new[] { npcIds.Count };
                for (int i = 0; i < diaologInfo.npcIds.Length; i++)
                {
                    diaologInfo.npcIds[i] = npcIds[i];
                }
                dialogDatas.Add(pointIndex, diaologInfo);
            }
        }

        public void UpdateDialogInfo(int pointIndex, int eventId)
        {
            if (dialogDatas.TryGetValue(pointIndex,out var dialogInfo))
            {
                dialogInfo.eventId = eventId;
                var npcEvent = PveUtils.GetNpcEvent(eventId);
                dialogInfo.npcNum = npcEvent.npcNum;
                if (dialogInfo.npcNum-1 != dialogInfo.npcIds.Length)
                {
                    Debug.LogError("npc数量匹配不对");
                }
                int dialogId = npcEvent.dialogId;
                var dialogConfig = PveUtils.GetNpcDialog(dialogId);
                dialogInfo.UpdateDialogContent(dialogConfig);
            }
        }

        public NpcDialogInfo GetDialogInfo(int pointIndex)
        {
            dialogDatas.TryGetValue(pointIndex, out var dialogInfo);
            return dialogInfo;
        }
        public void RemoveDialog(int pointIndex)
        {
            dialogDatas.Remove(pointIndex);
        }

   
       
        public void ShowDialogUI(Vector3 position,string content,int npcId)
        {
            ResidentHandler.Inst.GetFunction("pveManager.ShowDialogUI").Action(position,
                content, NFMgr.DataManager.IsMainCharactor(npcId) ? 1:2);
        }

        public void ShowDialogLoadingUI(Transform position, float time, string iconName)
        {
            ResidentHandler.Inst.GetFunction("pveManager.ShowDialogLoadingUI").Action(position,
                time, iconName);
        }

        public void ClearEvent()
        {
            foreach (var data in dialogDatas.Values)
            {
                data.contentQueue.Clear();
            }
            dialogDatas.Clear();
        }

        
    }
}
